package com.zxct.wyserver.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Rect;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.FrameLayout;

import com.zxct.wyserver.Interface.UiInterface;
import com.zxct.wyserver.R;
import com.zxct.wyserver.util.StatusBarUtils;

import butterknife.ButterKnife;

/**
 * Created by lanyu on 2018/1/30.
 */

public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener, UiInterface {
    protected Activity context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setContentView(getLayoutId());

        //解决设置了全屏模式下，软键盘弹出顶布局功能无效。
        AndroidBug5497Workaround.assistActivity(this);

        context = this;
        ButterKnife.bind(this);
        initView();
        initListener();
        initData();
        registerCommonButton();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    /**
     * 在多个界面里都存在的按钮，在这个方法统一注册监听事件
     */
    private void registerCommonButton() {
//        View view1 = findViewById(R.id.back);
//        if (null != view1) {
//            view1.setOnClickListener(this);
//        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
//            case R.id.back:
//                // 统一处理返回键
//                finish();
//                break;
            default:
                processClick(view);
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        StatusBarUtils.setTranslucent(this);
        getResources();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    /**
     * 跳转界面的方法
     */
    public void openActivity(Context context, Class<?> pClass) {
        Intent intent = new Intent(context, pClass);
        startActivity(intent);
    }

    /**
     * 带数据跳转界面的方法
     */
    public void openActivity(Context context, Class<?> pClass, Bundle bundle) {
        Intent intent = new Intent(context, pClass);
        intent.putExtras(bundle);
        startActivity(intent);
    }



    /**解决系统改变字体大小的时候导致的界面布局混乱的问题*/
    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        Configuration config=new Configuration();
        config.setToDefaults();
        res.updateConfiguration(config,res.getDisplayMetrics() );
        return res;
    }

    //解决设置了全屏模式下，软键盘弹出顶布局功能无效。
    public static class AndroidBug5497Workaround {

        // For more information, see https://code.google.com/p/android/issues/detail?id=5497
        // To use this class, simply invoke assistActivity() on an Activity that already has its content view set.

        public static void assistActivity (Activity activity) {
            new AndroidBug5497Workaround(activity);
        }

        private View mChildOfContent;
        private int usableHeightPrevious;
        private FrameLayout.LayoutParams frameLayoutParams;

        private AndroidBug5497Workaround(Activity activity) {
            FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);
            mChildOfContent = content.getChildAt(0);
            mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                public void onGlobalLayout() {
                    possiblyResizeChildOfContent();
                }
            });
            frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();
        }

        private void possiblyResizeChildOfContent() {
            int usableHeightNow = computeUsableHeight();
            if (usableHeightNow != usableHeightPrevious) {
                int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();
                int heightDifference = usableHeightSansKeyboard - usableHeightNow;
                if (heightDifference > (usableHeightSansKeyboard/4)) {
                    // keyboard probably just became visible
                    frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;
                } else {
                    // keyboard probably just became hidden
                    frameLayoutParams.height = usableHeightSansKeyboard;
                }
                mChildOfContent.requestLayout();
                usableHeightPrevious = usableHeightNow;
            }
        }

        private int computeUsableHeight() {
            Rect r = new Rect();
            mChildOfContent.getWindowVisibleDisplayFrame(r);
            return (r.bottom - r.top);
        }

    }

}
